/**
 * Copyright 2011 Ivan Hristov
 *
 * hristov.iv [AT] gmail [DOT] com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 */
package com.google.code.johanna.jsmp
import org.junit.Test
import org.fest.assertions.Assertions._

/**
 *
 * @author Ivan Hristov
 *
 */
class TestPacket {

  @Test
  def shouldConstructMessagePacket() {
    //GIVEN
    val srcAddress: String = "src";
    val dstAddress: String = "dst";
    val payloadType: String = "payload-type";
    val payload: String = "payload";

    //WHEN
    val message: Message = new Message(dstAddress, srcAddress, payloadType, payload);

    //THEN
    assertThat(message.packetType).isEqualTo(PacketType.MESSAGE);
    assertThat(message.dstAddress).isEqualTo(dstAddress);
    assertThat(message.srcAddress).isEqualTo(srcAddress);
    assertThat(message.payloadType).isEqualTo(payloadType);
    assertThat(message.payload).isEqualTo(payload);
  }

  @Test
  def shouldConstructMessageErrorPacket() {
    //GIVEN
    val srcAddress: String = "src";
    val dstAddress: String = "dst"
    val payloadType: String = "payload-type";
    val payload: String = "payload";
  	val errorPayload: String = "error-payload";
    
    //WHEN
    val errorMesage: MessageError = new MessageError(dstAddress, srcAddress, payloadType, payload, errorPayload);

    //THEN
    assertThat(errorMesage.packetType).isEqualTo(PacketType.MESSAGE_ERROR);
    assertThat(errorMesage.dstAddress).isEqualTo(dstAddress);
    assertThat(errorMesage.srcAddress).isEqualTo(srcAddress);
    assertThat(errorMesage.payloadType).isEqualTo(payloadType);
    assertThat(errorMesage.payload).isEqualTo(payload);
  }

  @Test
  def shouldConstructQueryPacket() {
    //GIVEN
    val srcAddress: String = "src";
    val dstAddress: String = "dst"
    val payloadType: String = "payload-type";
    val payload: String = "payload";
    val queryId: Long = 1l;

    //WHEN
    val query: Query = new Query(dstAddress, srcAddress, queryId, payloadType, payload);

    //THEN
    assertThat(query.packetType).isEqualTo(PacketType.QUERY);
    assertThat(query.dstAddress).isEqualTo(dstAddress);
    assertThat(query.srcAddress).isEqualTo(srcAddress);
    assertThat(query.payloadType).isEqualTo(payloadType);
    assertThat(query.payload).isEqualTo(payload);
  }

  @Test
  def shouldConstructResponsePacket() {
    //GIVEN
    val srcAddress: String = "src";
    val dstAddress: String = "dst"
    val payloadType: String = "payload-type";
    val payload: String = "payload";
    val queryId: Long = 1l;

    //WHEN
    val response: Response = new Response(dstAddress, srcAddress, queryId, payloadType, payload);

    //THEN
    assertThat(response.packetType).isEqualTo(PacketType.RESPONSE);
    assertThat(response.dstAddress).isEqualTo(dstAddress);
    assertThat(response.srcAddress).isEqualTo(srcAddress);
    assertThat(response.payloadType).isEqualTo(payloadType);
    assertThat(response.payload).isEqualTo(payload);
  }

  @Test
  def shouldConstructQueryErrorPacket() {
    //GIVEN
    val srcAddress: String = "src";
    val dstAddress: String = "dst"
    val payloadType: String = "payload-type";
    val payload: String = "payload";
    val queryId: Long = 1l;
    val errorPayload: String = "error-payload"
      
    //WHEN
    val queryError: QueryError = new QueryError(dstAddress, srcAddress, queryId, payloadType, payload, errorPayload);

    //THEN
    assertThat(queryError.packetType).isEqualTo(PacketType.QUERY_ERROR);
    assertThat(queryError.dstAddress).isEqualTo(dstAddress);
    assertThat(queryError.srcAddress).isEqualTo(srcAddress);
    assertThat(queryError.payloadType).isEqualTo(payloadType);
    assertThat(queryError.payload).isEqualTo(payload);
  }
}